/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package deatax.display.miniboard;

import deatax.GameData;
import deatax.display.TileRenderer;
import javax.media.opengl.GL2;

/**
 *
 * @author nrechten
 */
public class DiceBowl extends MiniBoardTile {


    float[] speed;
    float[] positions;
    int[] value;
    float scale = 1;

    DiceRenderer renderer = new DiceRenderer();

    GameData data;

    public DiceBowl(int x, int y, GameData data,MiniBoard miniboard) {
        super(x, y, "dice",miniboard);
        this.data = data;
        this.height = 2;
        speed = new float[0];
    }

    public void roll(int[] values) {
        value = values;
        speed = new float[value.length*3];
        positions = TileRenderer.genPolygon(value.length, 2f);
        scale = 3f/value.length;
        for (int i = 0; i < speed.length; i++) {
            speed[i] = 10000;
        }
    }

    public boolean checkArray(int[] a, int[] b){
        if(a == null || b == null || a.length != b.length) return false;
        for (int i = 0; i < b.length; i++) {
            if(a[i] != b[i]) return false;
        }
        return true;
    }
    
    public void draw(GL2 gl){

        super.draw(gl);
        
//        if(!checkArray(value, data.getDice().current())){
//            value = new int[data.getDice().current().length];
//            System.arraycopy(data.getDice().current(), 0, value, 0, value.length);
//            speed = new float[value.length*3];
//            positions = TileRenderer.genPolygon(value.length, 2f);
//            scale = 3f/value.length;
//            roll();
//        }

        for (int i = 0; i < speed.length; i++) {
            if(speed[i] > 0.1f){
                speed[i] -= speed[i] * (float)Math.random() * 0.3f;
            } else {
                speed[i] = 0;
            }
        }
        for (int i = 0, j = 0, k = 0; i < speed.length; i+=3) {
            gl.glPushMatrix();
            gl.glTranslatef(positions[j], positions[j+1], height+2);
            gl.glRotatef(speed[i], 1, 0,0);
            gl.glRotatef(speed[i+1], 0, 1,0);
            gl.glRotatef(speed[i+2], 0, 0,1);
            rotate(value[k],gl);
            gl.glScalef(scale, scale, scale);
            renderer.draw(gl);
            gl.glPopMatrix();
            j+=2; k++;
        }
    }
    
    
    public void rotate(int number, GL2 gl){
        switch(number){
            case 1: gl.glRotatef(-90, 1, 0, 0); break;
            case 2: gl.glRotatef(-90, 0, 1, 0); break;
            case 3: gl.glRotatef(180, 1, 0, 0); break;
            case 4: gl.glRotatef(90, 0, 1, 0); break;
            case 6: gl.glRotatef(90, 1, 0, 0); break;
        }
    }
}


